package cn.itcast.controller.role;

import cn.itcast.base.BaseController;
import cn.itcast.domain.Module;
import cn.itcast.domain.Role;
import cn.itcast.service.module.ModuleService;
import cn.itcast.service.role.RoleService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;


    @RequestMapping(name = "pageHelper插件分页查询角色",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        PageInfo pb = roleService.findAll(pageNum, pageSize, company_id);
        request.setAttribute("pb",pb);
        return "system/role/role-list";

    }

    @RequestMapping(name = "到新建角色页面",value = "/toAdd")
    public String toAdd(){
        return "system/role/role-add";
    }


    @RequestMapping(name = "角色保存/角色更新",value = "/edit")
    public String edit(Role role){

        //指定当前角色是属于哪个公司的
        role.setCompanyId(company_id);
        role.setCompanyName(company_name);

        if(UtilFuns.isEmpty(role.getId())){
            // 新增
            role.setId(UUID.randomUUID().toString());
            roleService.save(role);
        }else{
            // 更新
            roleService.update(role);

        }

        return "redirect:/system/role/list.do";
    }



    @RequestMapping(name = "到修改角色页面回显数据",value = "/toUpdate")
    public String toUpdate(String id){
        Role role=roleService.findById(id);
        request.setAttribute("role",role);

        return "system/role/role-update";
    }


    @RequestMapping(name = "角色数据删除",value = "/delete")
    public String delete(String id){

        roleService.delete(id);

        return "redirect:/system/role/list.do";
    }


    @RequestMapping(name = "到角色分配权限的页面",value = "/roleModule")
    public String roleModule(String roleid){
        // 根据点击的角色id查询该角色对象
        Role role = roleService.findById(roleid);
        // 带着角色对象到页面展示
        request.setAttribute("role",role);
        return "/system/role/role-module";
    }



    @RequestMapping(name = "ztree数据整理展示",value = "/initZtree")
    //ajax异步和服务器交互
    public @ResponseBody List<Map> initZtree(String rid){

        //查询当前点击角色所具备的所有权限 rid (角色的所有权限)
        List<Module> roleModuleList=moduleService.findRoleModule(rid);
        // 查询所有的菜单模块 （全部权限）
        List<Module> moduleList = moduleService.findModule();


        // 自定义给ajax的list<Map>
        List<Map> mapList=new ArrayList();
        for (Module module : moduleList) {
            Map map = new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

            if(roleModuleList.contains(module)){
                map.put("checked",true); //让ztree的复选框选中
            }

            mapList.add(map);
        }
        // 将自定义的List<Map>转成json格式：[{id:111, pId:11, name:"叶子节点111"},{ id:111, pId:11, name:"叶子节点111"}]
        return  mapList;
    }



    @RequestMapping(name = "设置角色的权限",value = "/updateRoleModule")
    public String updateRoleModule(String roleid,String moduleIds){

        moduleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }


}
